package com.example.saveword.save_word;

import com.example.saveword.R;
import com.example.saveword.dao.UserDao;
import com.example.saveword.utils.ViewUtil;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener,View.OnFocusChangeListener {
    ImageView btn_backToLogin;
    TextView btn_register;
    EditText et_register_username, et_register_qqNumber, et_register_pwd;

    UserDao userDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        init();
    }

    private void init() {
        btn_backToLogin = findViewById(R.id.btn_backToLogin);
        btn_register = findViewById(R.id.btn_register);
        et_register_username = findViewById(R.id.et_register_username);
        et_register_pwd = findViewById(R.id.et_register_pwd);
        et_register_qqNumber = findViewById(R.id.et_register_qqNumber);

        btn_backToLogin.setOnClickListener(this);
        btn_register.setOnClickListener(this);
        et_register_username.setOnFocusChangeListener(this);
        et_register_pwd.setOnFocusChangeListener(this);
        et_register_qqNumber.setOnFocusChangeListener(this);

        userDao = new UserDao(this);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.btn_backToLogin) {
            Intent intent = new Intent();
            intent.setClass(this, LoginActivity.class);
            startActivity(intent);
        } else if(view.getId() == R.id.btn_register) {
            String username = et_register_username.getText().toString();
            String password = et_register_pwd.getText().toString();
            String phone = et_register_qqNumber.getText().toString();
            if(username.equals("")) {
                Toast.makeText(this, "请输入用户名", Toast.LENGTH_LONG).show();
            } else if(phone.equals("")) {
                Toast.makeText(this, "请输入QQ号", Toast.LENGTH_LONG).show();
            } else if(password.equals("")) {
                Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
            } else {
                System.out.println(username + phone + password);
                int code = userDao.register(username, phone, password);
                if(code == 200) {
                    Toast.makeText(this, "注册成功，请重新登录", Toast.LENGTH_LONG).show();
                    // 跳转到登录页
                    Intent intent = new Intent();
                    intent.setClass(this, LoginActivity.class);
                    startActivity(intent);
                } else if(code == 500) {
                    Toast.makeText(this, "用户名已存在", Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(this, "注册失败，请重试", Toast.LENGTH_LONG).show();
                }
            }
        }
    }

    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        // 失去焦点时关闭软键盘
        if(!hasFocus) {
            ViewUtil.hideOneInputMethod(RegisterActivity.this, view);
        }
    }
}